<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/12
 * Time: 上午09:52
 */

class AhpUploadController extends BaseController {


    /**
     * @Author:          sqq
     * @DateTime:        2018-07-12 上午09:51
     * @Description:     ahp图片上传
     * @param string $token             用户token
     * @param file $file                图片
     * @param int $file_type            文件类型 (1:图片;2:非图片;)
     * @link http://api.anhui.com/ahp/ahp3.0/AhpUploadFile?token=7b388e638f99e6e7364e70fc32c34258
     */
    public function AhpUploadFile() {
        header("Content-Type: text/html; charset=utf-8");
        header('Access-Control-Allow-Origin:*');	//解决资源跨域问题
        header('Access-Control-Allow-Credentials', 'true');
        header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Headers', 'X-Requested-With, X_Requested_With');
        header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        header('Access-Control-Allow-Credentials', 'true');
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'file'=> 'required',   //最大上传2M
                //'file_type'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
//            if($validator->fails()) {
//                $response = Lang::get("ahp_error.args_error_1000");
//                $response["result"] = $validator->messages()->all();
//                break;
//            }
            $res = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($res)){
                break;
            }
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }

            $admin_id       = $res->id;             //管理员id
            $upload_file    = Input::file('file');          //上传文件
            $file_type      = Input::get('file_type');      //文件类型 (1:图片;2:非图片;)
            if($upload_file->isValid()){
                //文件上传成功
                $name = $upload_file->getClientOriginalName();  //文件原名称

                //$extension = $upload_file->getClientOriginalExtension(); //扩展名
                $res = substr($name,strripos($name,".")+1);
                $storage_name = date('Ymdhis', time()).'.'.$res;               //存储名称
                $upload_file->move(Config::get('config.ahp_upload_files'), $storage_name);
                $file['storage_name'] = Config::get('config.ahp_upload_files_url').$storage_name;
                $file['name'] = $storage_name;
            } else {
                $response = Lang::get("ahp_error.ahp_file_error_30010");
                break;
            }

            $response = [
                'code' => 0,
                'msg' => 'ok',
                'result' => $file
            ];
        }while(false);

        Log::info("info", array("system" => "ahp" ,"event" => "AhpUploadFile", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }



    /**
     * @Author:      sqq
     * @DateTime:    2018-09-10 13:48:28
     * @Description: 门店--图片上传
     * @param  $token  String   用户token
     * @param  $file_type int   类型
     * @link:
     */
    public function UploadFile(){
        header("Content-Type: text/html; charset=utf-8");
        header('Access-Control-Allow-Origin:*');	//解决资源跨域问题
        header('Access-Control-Allow-Credentials', 'true');
        header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Headers', 'X-Requested-With, X_Requested_With');
        header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        header('Access-Control-Allow-Credentials', 'true');
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'file'=> 'required',   //最大上传2M
                'file_type'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $res = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($res)){
                break;
            }
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }

            $upload_file = Input::file('file');
            $file_type = Input::get('file_type');
            $file = [];
            if($upload_file->isValid()){
                //文件上传成功
                $name = $upload_file->getClientOriginalName();  //文件原名称
                $res = substr($name,strripos($name,".")+1);
                $storage_name = date('Ymdhis', time()).'.'.$res;               //存储名称
                switch ($file_type) {
                    case 'store_image':
                        $upload_file->move(Config::get('store_wechat3.store_wechat3_file'), $storage_name);
                        $file['storage_name'] = Config::get('store_wechat3.store_wechat3_file_url').$storage_name;
                        $file['url'] = Config::get('store_wechat3.store_wechat3_file_url');
                        $file['name'] = $storage_name;
                        break;
                    default:
                        $response = Lang::get("ahp_error.ahp_file_error_30010");
                        break;
                }
            }else{
                $response = Lang::get("ahp_error.ahp_file_error_30010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $file);
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "UploadFile", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}